/*----------------------------------------------------------------------------
Owner(s): Shane Whitfield
$Copyright: $
$Id: $
----------------------------------------------------------------------------*/

#include "timermgr.h"
#include "render.h"
#include "texture.h"
#include "intro.h"
#include "bbmath.h"
#include "camera.h"
#include "mathutils.h"
#include "world.h"
#include "map.h"
#include "touchmgr.h"

/*--------------------------------------------------------------------------*/
CIntro::CIntro() : super("CIntro")
{
	m_state = EState_SettingUp;
	m_tState = 0.0f;
	m_curStartAlpha = 0.0f;
	m_fadingInStart = 0;
	m_titleTexture = g_texturemgr.CreateTexture("title.png");
	m_titleBackground = g_texturemgr.CreateTexture("title_bg.png");
	m_pressStartTexture = g_texturemgr.CreateTexture("press_start.png");
	
	m_worldMat.SetPos(Vector2f::Zero);
	m_worldMat.SetScale(1.0f);

}

void CIntro::Activate()
{
	m_state = EState_SettingUp;
	m_tState = 0.0f;
	m_curStartAlpha = 0.0f;
	m_fadingInStart = 0;
	
	g_camera.SetPos(Vector2f::Zero);
	g_camera.SetZoomFactor(1.0f);
}

void CIntro::RenderTitleBg()
{
	Vector2f mmin(-1.0,-1.4f), mmax(1.0f, 1.5f);
	g_texturemgr.EnableTextures(true);
	m_titleBackground->Apply();
	m_worldMat.PushMatrix();
	BasicDrawObjects::DrawTexturedRect(mmin, mmax, Color4f(), Vector2f(0.0f, 0.0f), Vector2f(1.0f, 1.0f));
	m_worldMat.PopMatrix();
	g_texturemgr.EnableTextures(false);
}

void CIntro::RenderTitle()
{
	Vector2f mmin(-1.0f,-1.3), mmax(1.0f,-0.8f);
	g_texturemgr.EnableTextures(true);
	m_titleTexture->Apply();
	m_worldMat.PushMatrix();
	BasicDrawObjects::DrawTexturedRect(mmin, mmax, Color4f(), Vector2f(0.0f, 0.0f), Vector2f(1.0f, 1.0f));
	m_worldMat.PopMatrix();
	g_texturemgr.EnableTextures(false);
}

void CIntro::RenderStart()
{
	Vector2f mmin(-1.0f, 0.3f), mmax(1.0f, 0.8f);
	g_texturemgr.EnableTextures(true);
	m_pressStartTexture->Apply();
	m_worldMat.PushMatrix();
	BasicDrawObjects::DrawTexturedRect(mmin, mmax, Color4f(1.0f, 0.0f, 0.0f, m_curStartAlpha), Vector2f(0.0f, 0.0f), Vector2f(1.0f, 1.0f));
	m_worldMat.PopMatrix();
	g_texturemgr.EnableTextures(false);
}

void CIntro::Render()
{
	switch (m_state)
	{
	case EState_ShowTitle:
		{
			RenderTitleBg();
			RenderTitle();
		}
		break;
	case EState_PressStart:
		{
			RenderTitleBg();
			RenderTitle();
			RenderStart();
		}
		break;
	}
}

void CIntro::Update()
{
	
	for(;;)
	{	
		EState prevState = m_state;
		switch (m_state)
		{
		case EState_SettingUp:
			{
				setState(EState_ShowTitle);
			}
			break;
		case EState_ShowTitle:
			{
				//wait two seconds... then show press start
				if ((T() - m_tState) >= 0.0f )
				{
					setState(EState_PressStart);
				}
			}
			break;
		case EState_PressStart:
			{
					//if we are at 0 then start fading in...
				//else if we are 1.0 then start fading out..
				if (g_touchmgr.HasTouchEvent(ETouchType_SingleTouchStart))
				{
					setState(EState_InActive);
					g_mapmgr.RequestMap();
				}
				else if (IsNearZero(m_curStartAlpha))
				{
					m_fadingInStart = true;
				}
				else if (IsNear(m_curStartAlpha, 1.0f))
				{
					m_fadingInStart = false;
				}
				
				if (m_fadingInStart)
				{
					m_curStartAlpha = BBMIN(m_curStartAlpha + (2.0f * Dt()), 1.0f);
				}
				else
				{
					m_curStartAlpha = BBMAX(m_curStartAlpha - (2.0f * Dt()), 0.0f);
				}
				
			}
			break;
		}
		
		if (prevState == m_state)
		{
			break;
		}
	}
}

void CIntro::setState(EState i_state)
{	
	switch(m_state)
	{
	case EState_PressStart:
		{
			g_mapmgr.RequestMap();
		}
		break;
	}
	
	m_state = i_state;
	m_tState = T();
}

/*-----------------------------------EOF!-----------------------------------*/ 
